package com.htfyun.eink.pw.app;

import android.Manifest;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.htfyun.eink.pw.PWHelper;
import com.htfyun.eink.pw.app.databinding.ActivityMainBinding;
import com.htfyun.eink.pw.core.EinkHelper;
import com.htfyun.eink.pw.core.ctrl.PWCoreHelper;
import com.htfyun.eink.pw.core.enums.PWAction;
import com.htfyun.eink.pw.draw.pen.PenRandomEraser;
import com.htfyun.eink.pw.draw.pen.PenRandomEraserPath;
import com.htfyun.eink.pw.draw.pen.epd.PenEPDFountain;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.whiteBoardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("PWDraw", "onClick");
            }
        });
        initPW();

        registerReceiver();
    }

    private void registerReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("logMotionEventDetails_enable");
        filter.addAction("logMotionEventDetails_disable");
        getActivity().registerReceiver(receiver, filter);
    }

    private void initPW() {
        PWHelper.bindView(binding.whiteBoardView);

        binding.tvDraw.setOnClickListener(this::onClick);
        binding.tvErase.setOnClickListener(v -> {
            PWHelper.pwConfig.configPen.setEraserPenId(PenRandomEraser.PEN_ID);
            Toast.makeText(getActivity(), "擦除", Toast.LENGTH_LONG).show();
        });
        binding.tvDrawStrokeWidthAdd.setOnClickListener(v -> {
            float strokeWidth = PWHelper.pwConfig.configPen.getWriteStrokeWidth();
            strokeWidth += 0.1f;
            PWHelper.pwConfig.configPen.setWriteStrokeWidth(strokeWidth);

            Toast.makeText(getActivity(), "Add " + strokeWidth, Toast.LENGTH_LONG).show();

        });
        binding.tvDrawStrokeWidthSub.setOnClickListener(v -> {
            float strokeWidth = PWHelper.pwConfig.configPen.getWriteStrokeWidth();
            strokeWidth -= 0.1f;
            if (strokeWidth < 0.2f) {
                strokeWidth = 0.2f;
            }
            PWHelper.pwConfig.configPen.setWriteStrokeWidth(strokeWidth);
            Toast.makeText(getActivity(), "Sub " + strokeWidth, Toast.LENGTH_LONG).show();

        });
        binding.tvClear.setOnClickListener(v -> {
            PWHelper.pwConfig.configCommand.clearAllBmp();
            EinkHelper.forceEinkFullUpdate();
            Toast.makeText(getActivity(), "清屏", Toast.LENGTH_LONG).show();
            PWCoreHelper.setPWAction(PWAction.CLEAR_FSB1);
        });

        binding.tvFullUpdate.setOnClickListener(v -> {
            EinkHelper.forceEinkFullUpdate();
        });

        PWHelper.pwConfig.configPointer.setInvalidateViewWhenUp(true);
        PWHelper.pwConfig.configPointer.setAntiAliasForPaint(false);
        PWHelper.pwConfig.configLog.setShowRectOfUpdatePW(false);
        PWHelper.pwConfig.configPen.setEraserPenIdOfCap(PenRandomEraserPath.PEN_ID);
        PWHelper.pwConfig.configPen.setWritePenId(PenEPDFountain.PEN_ID);

    }

    private Activity getActivity() {
        return this;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        String[] permissions = new String[]{
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
        };
        ActivityCompat.requestPermissions(this, permissions, 1000);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case "logMotionEventDetails_enable":
                    PWHelper.pwConfig.configLog.setLogMotionEventDetails(true);
                    break;
                case "logMotionEventDetails_disable":
                    PWHelper.pwConfig.configLog.setLogMotionEventDetails(false);
                    break;
            }
        }
    };

    private void onClick(View v) {
        Toast.makeText(getActivity(), "选择笔型", Toast.LENGTH_LONG).show();

        CharSequence[] items = new CharSequence[PenTypeEnum.values().length];
        int index = 0;
        for (PenTypeEnum typeEnum : PenTypeEnum.values()) {
            items[index++] = typeEnum.getNames();
        }
        AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity(), R.style.DialogStyle);
        dialog.setTitle("选择笔型");
        dialog.setItems(items, (dialog1, which) -> {
            PenTypeEnum typeEnum = PenTypeEnum.values()[which];
            Toast.makeText(getActivity(), typeEnum.getNames(), Toast.LENGTH_LONG).show();
            binding.tvDraw.setText(typeEnum.getNames());

            PWHelper.pwConfig.configPen.setWritePenId(typeEnum.getPenId());
        });
        dialog.show();

    }
}
